﻿package audio;
import java.util.HashMap;
import audio.Audio;
import audio.Music;
import audio.SoundFile;



public class GameMedia 
{
	private static Audio audio = new Audio();
	
	//store sound file and its id
	private static HashMap<Integer, SoundFile> soundMap;
	
	//bg music file name
	private static String musicFile;
	
	//bg music is mute or not
	public static boolean isMusicMute = false;//音乐静音
//	public static boolean mute = false;//音效静音
	//bg music volume
	public static float musicVolume = 3 ;
	
	public GameMedia()
	{
	
	}
	
	/**
	 * load all sound effect files
	 * 
	 * @param soundID an array of sound integer ID
	 * @param soundName an array of sound name
	 */
	public static void loadSounds(int[] soundID, String[] soundName)
	{
		int length = soundID.length;
		soundMap = new HashMap<Integer, SoundFile>(length);
		
		for(int i = 0; i < length; i++)
		{
			soundMap.put(soundID[i], audio.createSoundFile("audio/"+soundName[i]));
		}
	}
	/**
	 * load all sound effect files
	 * 
	 * @param soundID an array of sound integer ID
	 *
	 */
	public static void loadSounds(int[] soundID)
	{
		int length = soundID.length;
		soundMap = new HashMap<Integer, SoundFile>(length);
		
		for(int i = 0; i < length; i++)
		{
			soundMap.put(soundID[i], audio.createSoundFile(soundID[i]));
		}
	}
	
	/**
	 * play single sound
	 * 
	 * @param id sound ID
	 * @param isLoop loop or not
	 */
	public static void playSound(int id, boolean isLoop)
	{
//		Log.dln("Audio.mute="+Audio.mute);
		if(Audio.mute){
			return;
		}
		SoundFile sound = soundMap.get(id);
		if(isLoop)
		{
			sound.playContinuous();
		}
		else
		{
			try {
				sound.play();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * play background music
	 * 
	 * @param file music name
	 * @param isLoop loop or not
	 * @param b_play play immediately or not
	 */
	public static void playMusic(String file, boolean isLoop,boolean b_play)
	{
		musicFile = file;
		if(!isMusicMute)
		{
			Music.stop();
			Music.play(file, isLoop,b_play);
			Music.getMediaPlayer().setVolume(musicVolume, musicVolume);
		}
	}
	
	/** pause background music */
	public static void pauseMusic()
	{
		Music.pause();
	}
	
	/** resume background music */
	public static void resumeMusic()
	{
		if(!isMusicMute){
			Music.play();
		}
	}
	
	/**
	 * set sound effect mute or not
	 * 
	 * @param mute true is mute
	 */
	public static void muteSound(boolean mute)
	{
		Audio.mute = mute;
		
	}
	
	/**
	 * set music mute or not
	 * 
	 * @param mute true is mute
	 */
	public static void muteMusic(boolean mute)
	{
		isMusicMute = mute;
		if(mute)
		{
		
				if(Music.getMediaPlayer() != null
						&&Music.getMediaPlayer().isPlaying()){
					Music.pause();
//					Music.stop();
//					Music.getMediaPlayer().release();
				}
					
		}
		else
		{
			if(Music.getMediaPlayer()==null){
				Music.play(musicFile, true);
				Music.getMediaPlayer().setVolume(musicVolume, musicVolume);
			}
			else{
				Music.play();
			}
		}
	}
	
	/**
	 * set sound volume
	 * 
	 * @param volume range[0,1]
	 */
	public static void setSoundVolume(float volume)
	{
		audio.setMasterVolume(volume);
	
	}
	
	/**
	 * set music volume
	 * 
	 * @param volume range[0,1]
	 */
	public static void setMusicVolume(float volume)
	{
		
		if(Music.getMediaPlayer() != null)
		Music.getMediaPlayer().setVolume(volume, volume);
	
	}
	
	/** release instances, garbage collection */
	public static void dealloc()
	{
		try 
		{
			audio.destroy();
			Music.stop();
			Music.getMediaPlayer().release();
			soundMap.clear();
			audio = null;
			soundMap = null;	
			musicFile = null;
		} 
		catch (NullPointerException e) 
		{
		}
	}

	public static boolean getSoundmute() {//得到音效状态
		// TODO Auto-generated method stub
		return 	Audio.mute ;
	}
	public static boolean getMusicmute() {//得到音效状态
		// TODO Auto-generated method stub
		return 	isMusicMute ;
	}
}
